/**
 * 管理员功能管理页面
 * @author 韩睿倩
 */

<template>
  <div>
    <el-table :data="functionList" stripe border>
      <el-table-column type="index" label="标号" width="100"></el-table-column>
      <el-table-column prop="functionId" label="functionId" width="200">
      </el-table-column>
      <el-table-column prop="functionName" label="功能名称" width="300">
      </el-table-column>
      <el-table-column prop="functionStatus" label="状态" width="180">
        <template slot-scope="scope">
          <!-- {{ scope.row }} -->
          <!-- 作用域插槽 -->
          <el-switch v-model="scope.row.functionStatus" active-color="#13ce66" inactive-color="#ff4949"
            @change="functionStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- {{ scope.row }} -->
          <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="handleEdit(scope.row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="functionInfo.page" :page-size="functionInfo.pageSize" layout=" prev, pager, next, jumper"
        :page-count="pageCount">
      </el-pagination>
    </div>

    <!-- 修改功能名称的对话框 -->
    <el-dialog title="修改功能名称" :visible.sync="dialogVisible" width="50%">
      <!-- :model="newFunctionName" :rules="rules" -->
      <el-form ref="newFunctionName" :rules="rules" label-width="100px" :model="newFunctionName">
        <el-form-item label="功能名称" prop="functionName">
          <el-input v-model="newFunctionName.functionName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editFunctionNameSuccess">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getFunctionList,
  functionStatusChange,
  functionNameChange,
} from "../../api/administrator/functionApi";
export default {
  data() {
    return {
      functionInfo: {
        query: "",
        page: 1,
        pageSize: 10,
      },
      newFunctionName: {
        functionId: '',
        functionName: ""
      },
      // functionId: '',
      // functionName: {},
      functionList: [
        {
          functionId: '',
          functionName: '',
        }
      ],
      pageCount: 0,
      dialogVisible: false,
      rules: {
        functionName: [
          { required: true, message: '请输入功能名称', trigger: 'blur' },
        ],
      },


    };
  },
  created() {
    this.getFunctionList();
  },
  computed: {
    // functionId() {
    //   return this.$store.getters.functionId
    // },
    // functionName() {
    //   return this.$store.getters.functionName
    // }

  },
  methods: {
    //获取功能列表
    getFunctionList() {
      getFunctionList(this.functionInfo)
        .then((res) => {
          console.log(res);
          this.functionList = res.data.list;
          this.pageCount = res.data.totalPageNum;
          this.page = res.data.page;
          this.pageSize = res.data.pageSize
        })
        .catch((err) => {
          console.log(err);
        });
    },


    //修改功能名称确定
    editFunctionNameSuccess() {

      // console.log(newFunctionName)
      this.$refs.newFunctionName.validate((valid) => {
        if (valid) {
          console.log('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      // console.log(this.functionId)
      // console.log(newFunctionName)
      this.dialogVisible = false;
      functionNameChange(this.newFunctionName.functionId, { "functionName": this.newFunctionName.functionName }).then((res) => {
        console.log(res)
        this.$message.success('修改功能名称成功')
        this.getFunctionList()
        // this.editForm = res.data
      }).catch((err) => {
        console.log(err)
      })
    },

    //修改功能名称对话框控制
    handleEdit(data) {
      // console.log(data)
      console.log("handleEdit into");
      // this.$store.dispatch("getFunctionId", functionId);
      // this.$store.dispatch("getFunctionName", functionName);
      this.newFunctionName.functionId = data.functionId
      this.newFunctionName.functionName = data.functionName
      console.log(this.newFunctionName)
      this.dialogVisible = true;
      // this.newFunctionName.functionName = functionName

    },


    //分页实现
    handleSizeChange(pageSize) {
      console.log(`每页 ${pageSize} 条`);
      this.functionInfo.pageSize = pageSize;
      this.getFunctionList();
    },
    handleCurrentChange(page) {
      console.log(`当前页: ${page}`);
      this.functionInfo.page = page;
      this.getFunctionList();
    },

    //功能状态修改
    functionStatusChange(data) {

      // functionId = this.functionInfo
      console.log(data.functionId);
      functionStatusChange(data.functionId)
        .then((res) => {
          console.log("修改成功")
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
          console.log("修改失败")
        });
    },
    // handleClose(done) {
    //   this.$confirm("确认关闭？")
    //     .then(() => {
    //       done();
    //     })
    //     .catch(() => {});
    // },
  },
};
</script>

<style lang="scss">
.el-table {
  margin: 30px;
  width: 1000px;
  font-size: 17px;
  align-items: center;
  justify-content: center;
  margin: auto;
  margin-top: 20px;

}
.el-pagination {
  white-space: nowrap;
  margin: 20px;
  color: #303133;
  font-weight: 700;
  text-align: center;
 
}
</style>